<?php
	class admin extends spModel{
		var $pk = "aid"; // 每个留言唯一的标志，可以称为主键
		var $table = "admins"; // 数据表的名称
		
		var $join = array(
	 		'admins_group' => array(
	 			'mapkey' => 'agid',
	 			'fkey' => 'group_id'
	 		)
	 	);
	 	
	 	var $addrules = array(
	        'available_useranme' => array('admin', 'checkUsername')
	    );
	       
	    var $validator = array(
			"rules" => array( // 规则
				'username' => array(  // 这里是对username的验证规则
					'required' => true, // username不能为空
					'minlength' => 5,  // username长度不能小于5
					'maxlength' => 15, // username长度不能大于15
					'available_useranme' => true // 可用用户名
				),		
				'password' => array(  // 这里是对密码的验证规则
	                'required' => true, // 密码不能为空
	                'minlength' => 6  // 密码长度不能小于6
	            ),
	            'password1' => array(
					'required' => true, //这里是对第二次输入的密码的验证规则
					'equalTo' => 'password' //要等于'password'，也就是要与上面的密码相等
				)   
			),
			"messages" => array( // 提示信息
				'username' => array(
					'required' => '用户名不能为空',
					'minlength' => '用户名不能少于5个字符',
					'maxlength' => '用户名不能大于15字符',
					'available_useranme' => '用户名已经被注册'
				),
		        'password' => array(
					'required' => '请填写密码',
					'minlength' => '密码不能少于6位字符'
				),
				'password1' => array(
					'required' => '请填写确认密码',
					'equalTo' => '两次输入的密码不一致'
				)
			)
		);
		
		public function checkUsername($username){
			if($this->find(array('username'=>$username))){
				return false;
			}	
			else{
				return true;
			}	
		}
		
		public function getValidatorJS() {
			$validator = $this->validator;
			
			$validator['rules']['username']['remote'] = spUrl("admins", "checkUsername");
			$validator['messages']['username']['remote'] = $validator['messages']['username']['available_useranme'];
			unset($validator['rules']['username']['available_useranme']);
			unset($validator['messages']['username']['available_useranme']);		
			return parent::getValidatorJS($validator);
		}
		
	    public function getValidatorJSedit() {
			$validator = $this->validator;
			unset($validator['rules']['username']);
			unset($validator['messages']['username']);
			unset($validator['rules']['password']['required']);
			unset($validator['messages']['password']['required']);
			unset($validator['rules']['password']['minlength']);
			unset($validator['messages']['password']['minlength']);
			unset($validator['rules']['password1']['required']);
			unset($validator['messages']['password1']['required']);
			$validator['rules']['password1']['minlength'] = 6;
			$validator['messages']['password1']['minlength'] = '密码不能少于6位字符';		
			return parent::getValidatorJS($validator);
		}
		
		public function spValidatoredit() {
			unset($this->addrules);
			unset($this->validator['rules']['username']);
			unset($this->validator['messages']['username']);
			unset($this->validator['rules']['password']['required']);
			unset($this->validator['messages']['password']['required']);
			unset($this->validator['rules']['password']['minlength']);
			unset($this->validator['messages']['password']['minlength']);			
			unset($this->validator['rules']['password1']['required']);
			unset($this->validator['messages']['password1']['required']);
			$this->validator['rules']['password1']['minlength'] = 6;
			$this->validator['messages']['password1']['minlength'] = '密码不能少于6位字符';				
			return $this;
		}
		
		public function checkadmin($isadmin){
			try{
				if($_SESSION["admin_user"]["isadmin"] != $isadmin)
					throw new Exception("您无权访问该页");
			}catch(Exception $e){
				if ($GLOBALS['G_SP']['view']['charset']){
					echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"\" />";
				}
				echo "<script type=text/javascript>";
				echo "alert(\"" . $e->getMessage() . "\");";
				echo "window.location.href=\"" . spUrl("main", "welcome") . "\";";
				echo "</script>";
			}
		}
	}
?>